package com.QK.cnstudy.entity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.QK.cnstudy.bean.Subject;

public class WrongSubject {
    private int id;
    private int sentenceId;
    private String answerIds;
    private int answer;

    private int wrongTimes;

    public void setInfoFromCursor(Cursor cursor) {
        int tmp;
        if ((tmp = cursor.getColumnIndex("id")) != -1) {
            setId(cursor.getInt(tmp));
        }
        if ((tmp = cursor.getColumnIndex("sentence_id")) != -1) {
            setSentenceId(cursor.getInt(tmp));
        }
        if ((tmp = cursor.getColumnIndex("answer_ids")) != -1) {
            setAnswerIds(cursor.getString(tmp));
        }
        if ((tmp = cursor.getColumnIndex("answer")) != -1) {
            setAnswer(cursor.getInt(tmp));
        }
        if ((tmp = cursor.getColumnIndex("wrong_times")) != -1) {
            setWrongTimes(cursor.getInt(tmp));
        }

    }

    public WrongSubject() {

    }

    public void saveToDB(SQLiteDatabase sqLiteDatabase) {

        if (getId() == -1) {
            sqLiteDatabase.execSQL(
                            "insert into wrong_subject(sentence_id,answer_ids,answer,wrong_times) values (?,?,?,?)",
                            new Object[] { getSentenceId(), getAnswerIds(), getAnswer(), 0 });
        } else {
            sqLiteDatabase.execSQL("update wrong_subject set wrong_times=? where id=?", new Object[] {
                            getWrongTimes() + 1, getId() });
        }
    }

    public WrongSubject(Subject subject) {
        this.sentenceId = subject.getSentenceId();
        this.answerIds = subject.getAnswerIds().toString();
        this.answer = subject.getAnswer();
        this.id = subject.getId();
    }

    public int getWrongTimes() {
        return wrongTimes;
    }

    public void setWrongTimes(int wrongTimes) {
        this.wrongTimes = wrongTimes;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getSentenceId() {
        return sentenceId;
    }

    public void setSentenceId(int sentenceId) {
        this.sentenceId = sentenceId;
    }

    public String getAnswerIds() {
        return answerIds;
    }

    public void setAnswerIds(String answerIds) {
        this.answerIds = answerIds;
    }

    public int getAnswer() {
        return answer;
    }

    public void setAnswer(int answer) {
        this.answer = answer;
    }

}
